package com.caojing.haolemai.ui.outlets.outletsdetail;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseActivity;
import com.caojing.haolemai.bean.outletsdetailsbean.OutletsDeatailsBean;
import com.caojing.haolemai.config.UrlConfig;
import com.caojing.haolemai.view.MyGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

public class OutletsDetailActivity extends BaseActivity implements OutletsDetailContract.View {

    @Bind(R.id.home_detail_ivBack)
    ImageView homeDetailIvBack;
    @Bind(R.id.home_detail_tvTitle)
    TextView homeDetailTvTitle;
    @Bind(R.id.home_detail_ivShare)
    ImageView homeDetailIvShare;
    @Bind(R.id.home_detail_tvSort)
    TextView homeDetailTvSort;
    @Bind(R.id.home_detail_tvDefault)
    TextView homeDetailTvDefault;
    @Bind(R.id.home_detail_tvSale)
    TextView homeDetailTvSale;
    @Bind(R.id.home_detail_tvPrice)
    TextView homeDetailTvPrice;
    @Bind(R.id.home_detail_tvDiscount)
    TextView homeDetailTvDiscount;
    @Bind(R.id.home_detail_ivHead)
    ImageView homeDetailIvHead;
    @Bind(R.id.home_detail_Gv)
    MyGridView homeDetailGv;
    @Bind(R.id.home_detail_sv)
    PullToRefreshScrollView homeDetailSv;

    private OutletsDeatailsBean outletsDeatailsBean1;

    private OutletsDetailContract.Presenter presenter;
    private String id;   //作为每个itme的详情的关联id


    private int pager = 1;
    private List<OutletsDeatailsBean.ResultsEntity.ResultsEntity1> totallist;
    private OutletsDeatailsBean.ResultsEntity.OutletsInfoEntity outletsInfo;
    private OutletsDetailGvAdapter adapter;

    private int PAGES = 1;

    /*初始化标志位*/
    private int index1 = 0, index2 = 0;
    /*默认排序参数*/
    private int ORDER_BY = 6;


    //    这里是复用了首页中的布局文件
    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_detail;
    }

    @Override
    protected void initView() {
        presenter = new OutletsDetailPresenter(this);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Log.i("tag", "detailactivity得到的数据" + id);
        homeDetailGv.setFocusable(false);
        homeDetailTvDefault.setTextColor(Color.rgb(215, 0, 87));

        homeDetailGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent1 = new Intent();
                //这里是需要将productid 作为跳转页面的的url参数
                String productId = totallist.get(i).getProductId();
                intent1.putExtra("productId", productId);
                Log.i("tag", "productactivity的参数" + productId);
                startActivity(intent1);
            }
        });


    }

    @Override
    protected void initData() {
        presenter.getVertical(id, PAGES + "", initParams());
        totallist = new ArrayList<>();

//            OutletsDeatailsBean.ResultsEntity.OutletsInfoEntity outletsInfo = outletsDeatailsBean1.getResults().getOutletsInfo();
        if (outletsInfo != null) {
            Glide.with(OutletsDetailActivity.this).load(outletsInfo.getImageIndex()).asBitmap().into(homeDetailIvHead);
            homeDetailTvTitle.setText(outletsInfo.getTitle());

        }
        adapter = new OutletsDetailGvAdapter(totallist, this);
        homeDetailGv.setAdapter(adapter);


        homeDetailSv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                homeDetailSv.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                PAGES++;
                presenter.getVertical(id, "" + PAGES++, initParams());
//                homeDetailSv.onRefreshComplete();
            }
        });


    }

    public Map<String, String> initParams() {
        Map<String, String> params = new HashMap<>();

        params.put(UrlConfig.Key.INSTOCK, UrlConfig.DefaultValue.INSTOCK);
        params.put(UrlConfig.Key.ORDER_BY, ORDER_BY + "");
        return params;
    }


    @Override
    public void onGetDetailBeanSucess(OutletsDeatailsBean outletsDeatailsBean) {
//        outletsDeatailsBean1 = outletsDeatailsBean;
        outletsInfo = outletsDeatailsBean.getResults().getOutletsInfo();

        totallist.addAll(outletsDeatailsBean.getResults().getResults());
        Log.i("tag", "=" + totallist.size());
//        OutletsDeatailsBean.ResultsEntity.OutletsInfoEntity outletsInfo = outletsDeatailsBean.getResults().getOutletsInfo();
//        Glide.with(OutletsDetailActivity.this).load(outletsInfo.getImageIndex()).asBitmap().into(homeDetailIvHead);
//        homeDetailTvTitle.setText(outletsInfo.getTitle());
//        adapter = new OutletsDetailGvAdapter(totallist, this);
//        homeDetailGv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
//           homeDetailSv.onRefreshComplete();

        Log.i("tag", "activity网络数据成功");

    }

    @Override
    public void onGetDetailBeanFail(String erro) {
        Log.i("tag", "fklasfjalsfjls" + erro);

    }

    @OnClick({R.id.home_detail_ivBack, R.id.home_detail_ivShare, R.id.home_detail_tvSort, R.id.home_detail_tvDefault, R.id.home_detail_tvSale, R.id.home_detail_tvPrice, R.id.home_detail_tvDiscount})
    public void onClick(View view) {
        showColor();
        totallist.clear();
        switch (view.getId()) {
            case R.id.home_detail_ivBack:
                finish();
                break;
            case R.id.home_detail_ivShare:
//                这里是分享的部分；

                break;
            case R.id.home_detail_tvSort:

                break;
            case R.id.home_detail_tvDefault:
                homeDetailTvDefault.setTextColor(Color.rgb(215, 0, 87));
                index1 = 0;
                index2 = 0;
                ORDER_BY = 6;
                initData();
                break;
            case R.id.home_detail_tvSale:
                index1 = 0;
                index2 = 0;
                homeDetailTvSale.setTextColor(Color.rgb(215, 0, 87));
                ORDER_BY = 0;
                break;
            case R.id.home_detail_tvPrice:
                homeDetailTvPrice.setTextColor(Color.rgb(215, 0, 87));
                if (index1 % 2 == 0) {
                    /*Component组件，Compound复合，合成*/
                    Drawable drawable = getResources().getDrawable(R.mipmap.up_red);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    homeDetailTvPrice.setCompoundDrawables(null, null, drawable, null);
                    ORDER_BY = 1;
                } else {
                    Drawable drawable = getResources().getDrawable(R.mipmap.up_red_down);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    homeDetailTvPrice.setCompoundDrawables(null, null, drawable, null);
                    ORDER_BY = 2;
                }
                index1++;


                break;
            case R.id.home_detail_tvDiscount:
                homeDetailTvDiscount.setTextColor(Color.rgb(215, 0, 87));
                if (index2 % 2 == 0) {

                    Drawable drawable1 = getResources().getDrawable(R.mipmap.up_red);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    homeDetailTvDiscount.setCompoundDrawables(null, null, drawable1, null);
                    ORDER_BY = 3;
                } else {
                    Drawable drawable1 = getResources().getDrawable(R.mipmap.up_red_down);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    homeDetailTvDiscount.setCompoundDrawables(null, null, drawable1, null);
                    ORDER_BY = 4;
                }
                index2++;
                break;
        }
        initData();

    }

    private void showColor() {
        homeDetailTvDefault.setTextColor(Color.BLACK);
        homeDetailTvSale.setTextColor(Color.BLACK);
        homeDetailTvPrice.setTextColor(Color.BLACK);
        homeDetailTvDiscount.setTextColor(Color.BLACK);

//        这是设置textview点击的时候设置新的图片
        Drawable drawable = getResources().getDrawable(R.mipmap.up_gray);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        homeDetailTvPrice.setCompoundDrawables(null, null, drawable, null);
        homeDetailTvDiscount.setCompoundDrawables(null, null, drawable, null);

    }


}
